<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Partner extends CI_Model {

	function getPagePartners($page){
    	$amount = 20;
    	$start = ($page-1)*20;
        $data = $this->db->get('partners', $amount, $start);
        return $data->result();
    }

     function addPartner($name, $tel, $email, $pass){
    	$data = array(
    		'name' => $name,
    		 'tel' => $tel,
    		 'email' => $email,
    		 'pass' => $pass,
    		 'cmid' => $this->session->userdata('mid')
    		 );
    	$this->db->insert('partners', $data);

    }

    function editPartner($id, $name, $tel, $email, $pass){
    	$this->db->where('id',$id);
    	$this->db->set('name', $name);
		$this->db->set('tel', $tel);
		$this->db->set('email', $email);
		$this->db->set('pass', $pass);
		$this->db->update('partners');
    }

    function partnerExist($email, $cipheredpass){
    	$array = array('email' => $email, 'pass' => $cipheredpass);
    	$this->db->where($array);
    	$query = $this->db->get('partners');
    	if($query->num_rows() > 0 ){
    		return $query->result();
    	}else{
    		return false;
    	}
    }

    function getPartnerById($id){
    	$this->db->where('id', $id);
    	$data = $this->db->get('partners');
        return $data->result();
    }

    function getPartnersIdNames(){
        $this->db->select('id, name');
        $data = $this->db->get('partners');
        return $data->result();
    }

    function pageCount(){
		$query = $this->db->get('partners');
		$rowcount = $query->num_rows();
		$pagecount = 0;
		if($rowcount % 20 == 0){
			$pagecount = intval($rowcount/20);
		}else{
			$pagecount = intval($rowcount/20)+1;
		}
		return $pagecount;
	} 
}